package com.csdj.t301.zfy;

import java.lang.reflect.Field;
import java.util.Map;

public class MapToBean {
    public static Object mapToBean(Map<String, String> map, String javaBean) {
        Object obj = null;
        try {
            Class<?> clazz = Class.forName(javaBean);
            obj = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for (String key : map.keySet()) {
                for (Field field : fields) {
                    if (field.getName().equals(key)) {
                        field.setAccessible(true);
                        field.set(obj, map.get(key));
                        System.out.println("key: " + key + "\nfield:" + field.getName());
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return obj;
    }
}